package cn.com.utt.unms.vo;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import cn.com.utt.common.util.UttTool;
import cn.com.utt.unms.pojo.UserRecord;

public class OnlineUser {

	private Long id;
	private String hostname;
	private String ip;
	private String mac;
	private String uptime;
	private String ssid;
	private String bssid;
	private String band;
	private String recv;
	private String sent;
	private String authType;
	private String phone;
	private String wx;
	private String rxrate;
	private String txrate;
	private String bitrate;
	private String group;
	private String os;

	private String startTime;
	private String endTime;

	public void convert(UserRecord record) {
		this.id = record.getId();
		this.hostname = record.getHostname();
		this.ip = record.getIp();
		this.mac = record.getMac();
		this.os = record.getOs();
		this.phone = record.getUsername();
		this.wx = record.getUsername();
		Byte at = record.getAuthType();
		switch (at) {
		case 0:
			this.authType = "无";
			break;
		case 1:
			this.authType = "一建认证";
			break;
		case 2:
			this.authType = "手机认证";
			break;
		case 3:
			this.authType = "账号认证";
			break;
		case 4:
			this.authType = "微信连wifi";
			break;
		case 5:
			this.authType = "微信密码认证";
			break;
		default:
			this.authType = "无";
			break;
		}
		this.bssid = record.getBssid();
		this.ssid = record.getSsid();
		this.band = record.getBand() == 1 ? " 2.4G" : "5G";
		Timestamp start = record.getStartTime();
		if (record.getEndTime() == null) {
			Timestamp now = new Timestamp(System.currentTimeMillis());
			this.uptime = getDistanceTime(start, now, "yyyy-MM-dd HH:mm:ss");
		} else {
			Timestamp end = record.getEndTime();
			this.uptime = getDistanceTime(start, end, "yyyy-MM-dd HH:mm:ss");
		}
		this.sent = UttTool.getPrintSize(record.getBytesRecv()); // 下行 下行流量
		this.recv = UttTool.getPrintSize(record.getBytesSent()); // 上行 上行流量
		this.rxrate = record.getRxrate() + "Kb/s"; // 下行速率
		this.txrate = record.getTxrate() + "Kb/s"; // 上行速率
		this.bitrate = "" + record.getBitrate();
		// byte br = record.getBitrate();
		// switch (br) {
		// case 0:
		// this.bitrate = "自动";
		// break;
		// case 1:
		// this.bitrate = "11M";
		// break;
		// case 2:
		// this.bitrate = "54M";
		// break;
		// case 3:
		// this.bitrate = "150M";
		// break;
		// case 4:
		// this.bitrate = "300M";
		// break;
		// }
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		this.startTime = formatter.format(record.getStartTime());
		if (record.getEndTime() != null) {
			this.endTime = formatter.format(record.getEndTime());
		}
	}

	public String getDistanceTime(Timestamp str1, Timestamp str2, String pattern) {
		DateFormat df = new SimpleDateFormat(pattern);
		Date one;
		Date two;
		long day = 0;
		long hour = 0;
		long min = 0;
		long sec = 0;
		try {
			one = df.parse(df.format(str1));
			two = df.parse(df.format(str2));
			long time1 = one.getTime();
			long time2 = two.getTime();
			long diff;
			if (time1 < time2) {
				diff = time2 - time1;
			} else {
				diff = time1 - time2;
			}
			day = diff / (24 * 60 * 60 * 1000);
			hour = (diff / (60 * 60 * 1000) - day * 24);
			min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
			sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return day + " D " + hour + " H " + min + " M " + sec + " S";
	}

	public Long getId() {
		return id;
	}

	public String getHostname() {
		return hostname;
	}

	public String getIp() {
		return ip;
	}

	public String getMac() {
		return mac;
	}

	public String getSsid() {
		return ssid;
	}

	public String getBssid() {
		return bssid;
	}

	public String getBand() {
		return band;
	}

	public String getAuthType() {
		return authType;
	}

	public String getRxrate() {
		return rxrate;
	}

	public String getTxrate() {
		return txrate;
	}

	public String getBitrate() {
		return bitrate;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setHostname(String hostname) {
		this.hostname = hostname;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public void setMac(String mac) {
		this.mac = mac;
	}

	public void setSsid(String ssid) {
		this.ssid = ssid;
	}

	public void setBssid(String bssid) {
		this.bssid = bssid;
	}

	public void setBand(String band) {
		this.band = band;
	}

	public void setAuthType(String authType) {
		this.authType = authType;
	}

	public void setRxrate(String rxrate) {
		this.rxrate = rxrate;
	}

	public void setTxrate(String txrate) {
		this.txrate = txrate;
	}

	public void setBitrate(String bitrate) {
		this.bitrate = bitrate;
	}

	public String getUptime() {
		return uptime;
	}

	public void setUptime(String uptime) {
		this.uptime = uptime;
	}

	public String getRecv() {
		return recv;
	}

	public String getSent() {
		return sent;
	}

	public void setRecv(String recv) {
		this.recv = recv;
	}

	public void setSent(String sent) {
		this.sent = sent;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public String getPhone() {
		return phone;
	}

	public String getWx() {
		return wx;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public void setWx(String wx) {
		this.wx = wx;
	}

	public String getStartTime() {
		return startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	
	public String getOs() {
		return os;
	}

	public void setOs(String os) {
		this.os = os;
	}

}
